<template>
  <div ref="chart" style="width: 100%; height: 600px;"></div>
</template>

<script>
import * as echarts from 'echarts';
import 'echarts-gl';

export default {
  name: 'Beamforming3DChart',
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chart = echarts.init(this.$refs.chart);
      const data = this.generateBeamformingData(16, 16); // 调整阵列大小（例如：8x8）

      const option = {
        tooltip: {},
        visualMap: {
          min: 0,
          max: 1,
          dimension: 1,
          inRange: {
            color: ['#00aaff', '#ffaa00', '#ff0000'], // 波束强度颜色渐变
          },
        },
        grid3D: {
          show: true,
          boxWidth: 100,
          boxHeight: 100,
          boxDepth: 100,
          viewControl: {
            projection: 'perspective',
            alpha: 30,
            beta: 30,
            zoomSensitivity: 1,
          },
          light: {
            main: {
              intensity: 1.2,
            },
            ambient: {
              intensity: 0.5,
            },
          },
        },
        xAxis3D: { type: 'value', name: 'X', min: -1, max: 1 },
        yAxis3D: { type: 'value', name: 'Y', min: -1, max: 1 },
        zAxis3D: { type: 'value', name: 'Z', min: 0, max: 1 },
        series: [
          {
            type: 'surface',
            wireframe: {
              show: false,
            },
            shading: 'lambert',
            data,
          },
        ],
      };

      chart.setOption(option);
    },

    generateBeamformingData(rows, cols) {
      const data = [];
      const numTheta = 180; // 仰角：从 0° 到 180°
      const numPhi = 360; // 方位角：从 0° 到 360°
      const d = 0.2; // 天线间距（适当加大，让波束更饱满）
      const k = 2 * Math.PI; // 波数
      const beamAngleTheta = Math.PI / 3; // 主波束方向（仰角）
      const beamAnglePhi = Math.PI / 4; // 主波束方向（方位角）

      // 计算方向性增强后的波束
      for (let theta = 0; theta <= numTheta; theta++) {
        for (let phi = 0; phi < numPhi; phi++) {
          const thetaRad = (theta / numTheta) * Math.PI; // 仰角弧度
          const phiRad = (phi / numPhi) * 2 * Math.PI; // 方位角弧度

          // 计算方向向量
          const dx = Math.sin(thetaRad) * Math.cos(phiRad);
          const dy = Math.sin(thetaRad) * Math.sin(phiRad);
          const dz = Math.cos(thetaRad);

          // 波束方向性函数
          let arrayFactor = 0;
          for (let m = 0; m < rows; m++) {
            for (let n = 0; n < cols; n++) {
              const deltaX = m * d; // 水平方向相位差
              const deltaY = n * d; // 垂直方向相位差
              const phaseShift =
                k *
                (deltaX * (dx - Math.sin(beamAngleTheta) * Math.cos(beamAnglePhi)) +
                  deltaY * (dy - Math.sin(beamAngleTheta) * Math.sin(beamAnglePhi)));
              arrayFactor += Math.cos(phaseShift);
            }
          }

          // 强度归一化，增强主瓣
          const intensity = Math.pow(Math.abs(arrayFactor) / (rows * cols), 2);

          // 转为笛卡尔坐标，适当放大波束范围
          const scale = 1; // 放大比例
          const x = scale * intensity * Math.sin(thetaRad) * Math.cos(phiRad);
          const y = scale * intensity * Math.sin(thetaRad) * Math.sin(phiRad);
          const z = scale * intensity * Math.cos(thetaRad);

          data.push([x, y, z]);
        }
      }

      return data;
    },
  },
};
</script>

<style scoped>
/* 样式部分 */
</style>
